package com.jiuair.cloud.data.revenue.controller;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jiuair.cloud.data.revenue.entity.CtripData;
import com.jiuair.cloud.data.revenue.result.PageParams;
import com.jiuair.cloud.data.revenue.result.Result;
import com.jiuair.cloud.data.revenue.service.CtripDataService;
import com.jiuair.cloud.data.revenue.utils.TripleDeaHelper;
import com.jiuair.cloud.data.revenue.vo.PushDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;

import java.util.List;
import java.util.Map;

/**
 * 前端控制器
 *
 * @author deng
 * @date 2020-08-18
 */
@Api(value = "", tags = "")
@RestController
@RequestMapping("ctripData")
public class CtripDataController {

    @Autowired
    private CtripDataService targetService;

    /**
     * 获取分页数据
     *
     * @return
     */
    @ApiOperation(value = "获取分页数据-", notes = "获取分页数据")
    @GetMapping(value = "/list")
    public Result<IPage<CtripData>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        QueryWrapper<CtripData> queryWrapper = new QueryWrapper();
        return Result.ok().data(targetService.page(pageParams, queryWrapper));
    }

    /**
     * 根据ID查找数据
     */
    @ApiOperation(value = "根据ID查找数据-", notes = "根据ID查找数据")
    @ResponseBody
    @RequestMapping("/get")
    public Result<CtripData> get(@RequestParam("id") Long id) {
        CtripData entity = targetService.getById(id);
        return Result.ok().data(entity);
    }

    /**
     * 添加数据
     *
     * @return
     */
    @ApiOperation(value = "添加数据-", notes = "添加数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "flightNumber", required = true, value = "航班号", paramType = "form"),
            @ApiImplicitParam(name = "fromCity", required = true, value = "起飞城市", paramType = "form"),
            @ApiImplicitParam(name = "toCity", required = true, value = "目的城市", paramType = "form"),
            @ApiImplicitParam(name = "fromData", required = true, value = "旅行日期(起飞日期)", paramType = "form"),
            @ApiImplicitParam(name = "fromTime", required = true, value = "起飞时刻", paramType = "form"),
            @ApiImplicitParam(name = "craftTypeCode", required = true, value = "机型", paramType = "form"),
            @ApiImplicitParam(name = "flyTime", required = true, value = "飞行时间", paramType = "form"),
            @ApiImplicitParam(name = "toTime", required = true, value = "到达时刻", paramType = "form"),
            @ApiImplicitParam(name = "kimNum", required = true, value = "公里数", paramType = "form"),
            @ApiImplicitParam(name = "fliProple", required = true, value = "旅客人数", paramType = "form"),
            @ApiImplicitParam(name = "layoutNumber", required = true, value = "布局数", paramType = "form"),
            @ApiImplicitParam(name = "layoutCnf", required = true, value = "布局数cnf", paramType = "form"),
            @ApiImplicitParam(name = "flyRevenue", required = true, value = "航班收入", paramType = "form"),
            @ApiImplicitParam(name = "plf", required = true, value = "客座率", paramType = "form"),
            @ApiImplicitParam(name = "plfCnf", required = true, value = "客座率cnf", paramType = "form"),
            @ApiImplicitParam(name = "hourRevenue", required = true, value = "小时收入", paramType = "form"),
            @ApiImplicitParam(name = "passengerKilRevenue", required = true, value = "客公里收入", paramType = "form"),
            @ApiImplicitParam(name = "seatKilRevenue", required = true, value = "座公里收入", paramType = "form"),
            @ApiImplicitParam(name = "seatKilRevenueCnf", required = true, value = "座公里收入cnf", paramType = "form")
    })
    @PostMapping("/add")
    public Result add(
            @RequestParam(value = "flightNumber") String flightNumber,
            @RequestParam(value = "fromCity") String fromCity,
            @RequestParam(value = "toCity") String toCity,
            @RequestParam(value = "fromData") String fromData,
            @RequestParam(value = "fromTime") String fromTime,
            @RequestParam(value = "craftTypeCode") String craftTypeCode,
            @RequestParam(value = "flyTime") String flyTime,
            @RequestParam(value = "toTime") String toTime,
            @RequestParam(value = "kimNum") String kimNum,
            @RequestParam(value = "fliProple") String fliProple,
            @RequestParam(value = "layoutNumber") String layoutNumber,
            @RequestParam(value = "layoutCnf") String layoutCnf,
            @RequestParam(value = "flyRevenue") String flyRevenue,
            @RequestParam(value = "plf") String plf,
            @RequestParam(value = "plfCnf") String plfCnf,
            @RequestParam(value = "hourRevenue") String hourRevenue,
            @RequestParam(value = "passengerKilRevenue") String passengerKilRevenue,
            @RequestParam(value = "seatKilRevenue") String seatKilRevenue,
            @RequestParam(value = "seatKilRevenueCnf") String seatKilRevenueCnf
    ) {
        CtripData entity = new CtripData();
        entity.setFlightNumber(flightNumber);
        entity.setFromCity(fromCity);
        entity.setToCity(toCity);
        entity.setFromData(fromData);
        entity.setFromTime(fromTime);
        entity.setCraftTypeCode(craftTypeCode);
        entity.setFlyTime(flyTime);
        entity.setToTime(toTime);
        entity.setKimNum(kimNum);
        entity.setFliProple(fliProple);
        entity.setLayoutNumber(layoutNumber);
        entity.setLayoutCnf(layoutCnf);
        entity.setFlyRevenue(flyRevenue);
        entity.setPlf(plf);
        entity.setPlfCnf(plfCnf);
        entity.setHourRevenue(hourRevenue);
        entity.setPassengerKilRevenue(passengerKilRevenue);
        entity.setSeatKilRevenue(seatKilRevenue);
        entity.setSeatKilRevenueCnf(seatKilRevenueCnf);
        targetService.save(entity);
        return Result.ok();
    }

    /**
     * 新增数据
     *
     * @return
     */
    @ApiOperation(value = "保存数据-", notes = "保存数据")
    @PostMapping("/save")
    @ResponseBody
    public Result add(@RequestBody CtripData ctripData) {
        Result Result = new Result();
        targetService.save(ctripData);
        return Result.ok();
    }


    /**
     * 新增数据
     *
     * @return
     */
    private String SecretKey = "460770ba3b1fea5948bfa23db46cb59bf4cb";
    private String key = "70ba3b1fea5948bfa23db46cb59bf4cb";


    @ApiOperation(value = "保存数据-", notes = "保存数据")
    @PostMapping("/PostAdd")
    @ResponseBody
    public Result PostAdd(@RequestBody PushDto ctripData) {
       /* JSONArray jsonArray=new JSONArray(dataList);
        String result = jsonArray.toString();*/
        String deskey = TripleDeaHelper.getDesKey(ctripData.getHead().getReqSign(), key, ctripData.getHead().getTimeStamp());
        String encrypt = TripleDeaHelper.desResult(ctripData.getContent(), deskey);
        return Result.ok().data(encrypt);
    }

    @ApiOperation(value = "保存数据-", notes = "保存数据")
    @PostMapping("/Post")
    @ResponseBody
    public Result Post(@RequestBody PushDto ctripData) {

        // String secretKey = TripleDeaHelper.getKey(SecretKey, Long.valueOf(ctripData.getHead().getTimeStamp()));
        String dataStr = TripleDeaHelper.decrypt(ctripData.getContent(), SecretKey);
        System.out.println(dataStr);
        JSONArray arr = JSONArray.parseArray(dataStr);
        List<CtripData> resultList = new ArrayList<>();
        if (arr != null && arr.size() >0) {
            for (int i = 0; i < arr.size(); i++) {
                CtripData ctripData1 = new CtripData();
                ctripData1.setFlightNumber(arr.getJSONObject(i).get("航班号").toString());
                ctripData1.setFromCity(arr.getJSONObject(i).get("始发").toString());
                ctripData1.setToCity(arr.getJSONObject(i).get("目的").toString());
                ctripData1.setFromData(arr.getJSONObject(i).get("旅行日期").toString());
                ctripData1.setFromTime(arr.getJSONObject(i).get("起飞时间").toString());
                ctripData1.setCraftTypeCode(arr.getJSONObject(i).get("机型").toString());
                ctripData1.setFlyTime(arr.getJSONObject(i).get("飞行时间").toString());
              //  ctripData1.setToTime(arr.getJSONObject(i).get("到达时间").toString());
                ctripData1.setKimNum(arr.getJSONObject(i).get("公里数").toString());
                ctripData1.setFliProple(arr.getJSONObject(i).get("旅客人数").toString());
                ctripData1.setLayoutNumber(arr.getJSONObject(i).get("布局数").toString());
             //   ctripData1.setLayoutCnf(arr.getJSONObject(i).get("布局数CNF").toString());
                ctripData1.setFlyRevenue(arr.getJSONObject(i).get("航班收入").toString());
                ctripData1.setPlf(arr.getJSONObject(i).get("客座率").toString());
             //   ctripData1.setPlfCnf(arr.getJSONObject(i).get("客座率CNF").toString());
                ctripData1.setHourRevenue(arr.getJSONObject(i).get("小时收入").toString());
                ctripData1.setPassengerKilRevenue(arr.getJSONObject(i).get("客公里收入").toString());
                ctripData1.setSeatKilRevenue(arr.getJSONObject(i).get("座公里收入").toString());
              //  ctripData1.setSeatKilRevenueCnf(arr.getJSONObject(i).get("座公里收入CNF").toString());
                resultList.add(ctripData1);
            }
        }
        targetService.saveBatch(resultList);
        //   TaskDataDto taskData = JSONObject.parseObject(dataStr, TaskDataDto.class);

     /*   if (taskData.getDataList() != null && taskData.getDataList().size() > 0) {

            DateFormat df = new SimpleDateFormat("yyyyMM");
            int re = cTripDataBiz.inserOrUpdateBatch(df.format(taskData.getDataList().get(0).getPlanTime()), taskData.getDataList());
            logger.info(String.format("[接收] [%s]航班数[%8d]", taskData.getTaskKey(),taskData.getDataList().size()));
            logger.info("[接收] " + taskData.logKey());
            logger.debug(String.format("[接收] 航班数[%8d]", taskData.getDataList().size()));
        }
*/
        /*  targetService.inserOrUpdateBatch(ctripData);*/
        return Result.ok();
    }


    /**
     * 更新数据
     *
     * @return
     */
    @ApiOperation(value = "更新数据-", notes = "更新数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, value = "", paramType = "form"),
            @ApiImplicitParam(name = "flightNumber", required = true, value = "航班号", paramType = "form"),
            @ApiImplicitParam(name = "fromCity", required = true, value = "起飞城市", paramType = "form"),
            @ApiImplicitParam(name = "toCity", required = true, value = "目的城市", paramType = "form"),
            @ApiImplicitParam(name = "fromData", required = true, value = "旅行日期(起飞日期)", paramType = "form"),
            @ApiImplicitParam(name = "fromTime", required = true, value = "起飞时刻", paramType = "form"),
            @ApiImplicitParam(name = "craftTypeCode", required = true, value = "机型", paramType = "form"),
            @ApiImplicitParam(name = "flyTime", required = true, value = "飞行时间", paramType = "form"),
            @ApiImplicitParam(name = "toTime", required = true, value = "到达时刻", paramType = "form"),
            @ApiImplicitParam(name = "kimNum", required = true, value = "公里数", paramType = "form"),
            @ApiImplicitParam(name = "fliProple", required = true, value = "旅客人数", paramType = "form"),
            @ApiImplicitParam(name = "layoutNumber", required = true, value = "布局数", paramType = "form"),
            @ApiImplicitParam(name = "layoutCnf", required = true, value = "布局数cnf", paramType = "form"),
            @ApiImplicitParam(name = "flyRevenue", required = true, value = "航班收入", paramType = "form"),
            @ApiImplicitParam(name = "plf", required = true, value = "客座率", paramType = "form"),
            @ApiImplicitParam(name = "plfCnf", required = true, value = "客座率cnf", paramType = "form"),
            @ApiImplicitParam(name = "hourRevenue", required = true, value = "小时收入", paramType = "form"),
            @ApiImplicitParam(name = "passengerKilRevenue", required = true, value = "客公里收入", paramType = "form"),
            @ApiImplicitParam(name = "seatKilRevenue", required = true, value = "座公里收入", paramType = "form"),
            @ApiImplicitParam(name = "seatKilRevenueCnf", required = true, value = "座公里收入cnf", paramType = "form")
    })
    @PostMapping("/update")
    public Result update(
            @RequestParam(value = "id") Long id,
            @RequestParam(value = "flightNumber") String flightNumber,
            @RequestParam(value = "fromCity") String fromCity,
            @RequestParam(value = "toCity") String toCity,
            @RequestParam(value = "fromData") String fromData,
            @RequestParam(value = "fromTime") String fromTime,
            @RequestParam(value = "craftTypeCode") String craftTypeCode,
            @RequestParam(value = "flyTime") String flyTime,
            @RequestParam(value = "toTime") String toTime,
            @RequestParam(value = "kimNum") String kimNum,
            @RequestParam(value = "fliProple") String fliProple,
            @RequestParam(value = "layoutNumber") String layoutNumber,
            @RequestParam(value = "layoutCnf") String layoutCnf,
            @RequestParam(value = "flyRevenue") String flyRevenue,
            @RequestParam(value = "plf") String plf,
            @RequestParam(value = "plfCnf") String plfCnf,
            @RequestParam(value = "hourRevenue") String hourRevenue,
            @RequestParam(value = "passengerKilRevenue") String passengerKilRevenue,
            @RequestParam(value = "seatKilRevenue") String seatKilRevenue,
            @RequestParam(value = "seatKilRevenueCnf") String seatKilRevenueCnf
    ) {
        CtripData entity = new CtripData();
        entity.setId(id);
        entity.setFlightNumber(flightNumber);
        entity.setFromCity(fromCity);
        entity.setToCity(toCity);
        entity.setFromData(fromData);
        entity.setFromTime(fromTime);
        entity.setCraftTypeCode(craftTypeCode);
        entity.setFlyTime(flyTime);
        entity.setToTime(toTime);
        entity.setKimNum(kimNum);
        entity.setFliProple(fliProple);
        entity.setLayoutNumber(layoutNumber);
        entity.setLayoutCnf(layoutCnf);
        entity.setFlyRevenue(flyRevenue);
        entity.setPlf(plf);
        entity.setPlfCnf(plfCnf);
        entity.setHourRevenue(hourRevenue);
        entity.setPassengerKilRevenue(passengerKilRevenue);
        entity.setSeatKilRevenue(seatKilRevenue);
        entity.setSeatKilRevenueCnf(seatKilRevenueCnf);
        targetService.updateById(entity);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @return
     */
    @ApiOperation(value = "删除数据-", notes = "删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, value = "id", paramType = "form")
    })
    @PostMapping("/remove")
    public Result remove(
            @RequestParam(value = "id") Long id
    ) {
        targetService.removeById(id);
        return Result.ok();
    }

    /**
     * 批量删除数据
     *
     * @return
     */
    @ApiOperation(value = "批量删除数据-", notes = "批量删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", required = true, value = "多个用,号隔开", paramType = "form")
    })
    @PostMapping("/batch/remove")
    public Result batchRemove(
            @RequestParam(value = "ids") String ids
    ) {
        targetService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok();
    }

}
